import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/master/master_index_bean.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/master/master_index_item.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/master_index_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:provider/provider.dart';


class MasterIndexWidget extends StatefulWidget {
  const MasterIndexWidget({Key key}) : super(key: key);

  @override
  _MasterIndexWidgetState createState() => _MasterIndexWidgetState();
}

class _MasterIndexWidgetState extends State<MasterIndexWidget> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: PreferredSize(
        child: Container(
          color: Theme.of(context).primaryColor,
          child: SafeArea(
            child: Center(
              child: Text(
                StringResources.da_shi_zhuo,
                style: TextStyle(
                    fontSize: 34.sp,
                    color: Theme.of(context).textTheme.subtitle1.color),
              ),
            ),
          ),
        ),
        preferredSize: Size(double.infinity, 95.h),
      ),
      body: Selector<MasterIndexViewModel, BaseState>(
          builder: (context, state, __) {
            if (state == BaseState.LOADING) {
              return LoadingWidget();
            }
            if (state == BaseState.EMPTY) {
              return EmptyWidget(content: '大师桌数据为空',);
            }
            if (state == BaseState.FAIL) {
              return FailWidget(
                onClick: () {
                  context.read<MasterIndexViewModel>().loadData();
                },
              );
            }
            if (state == BaseState.CONTENT) {
              FileShow data = context.select<MasterIndexViewModel, FileShow>(
                      (value) => value.bean);
              return Container(
                color: Theme.of(context).backgroundColor,
                child: RefreshIndicator(
                    child: MasterFileList(data: data),
                    onRefresh: () async {
                      context.read<MasterIndexViewModel>().loadData();
                    }),
              );
            }
            return Constants.empty;
          },
          selector: (_, model) => model.mState),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
